<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Symbol新类型</title>
    <script>
      /*
       * ES6增加了Symbol新的原始类型
       */
      let atr1 = String("Hello World");
      let str2 = String("HeloWorld");
      console.log(str1 == str2); //结果：true
      console.log(str1 === str2); //结果：true

      let s1 = Symbol("mySymbol");
      let s2 = Symbol("mySymbol");
      console.log(typeof s1); //结果：symbol
      console.log(s1.toString()); //结果：Symbol(mySymbol)
      console.log(s1 == s2); //结果：false
      console.log(s1 === s2); //结果：false
      // s1与s2表面上看着一样，其实可以理解为内含不同的id
      /*作为常量*/
      const Java = Symbol();
      const Ruby = Symbol();
      const Perl = Symbol();
      const Php = Symbol();
      const VB = Symbol();

      var lang = Php;

      if (lang === Java) {
        console.log("Java的未来在哪里？");
      }
      if (lang === Ruby) {
        console.log("再学个Ruby on Rails吧。");
      }
      /*作为属性*/
      let s1 = Symbol("mySymbol");
      let s2 = Symbol("mySymbol");

      var obj = {};
      obj[s1] = "hello";
      obj[s2] = "world";

      console.log(obj);
      //Symbol(mySymbol): "helo" Symbol(mySymbol): "world"
      console.log(obj[s1]); //hello
      console.log(obj[s2]); //world

      /* 半隐藏属性 */
      const MYKEY = Symbol();
      class User {
        constructor(key, name, age) {
          this[MYKEY] = key;
          this.name = name;
          this.age = age;
        }
        checkKEY(key) {
          return this[MYKEY] === key;
        }
      }

      let user = new User(123, "Curry", 29);
      console.log(user.name, user.age, user[MYKEY]);
      console.log(user.checkKEY(123)); //true
      console.log(user.checkKEY(456)); //false
      console.log(Object.keys(user)); //[ 'name', 'age' ]
      console.log(JSON.stringify(user)); //{"name":"Curry","age":29}
    </script>
  </head>
  <body></body>
</html>
